/**
 * Created by wzh on 2016/8/16.
 */
'use strict'
swan_pc.controller("BusinessInfoController",function($q,$scope,$rootScope,Membership,Member,EntityOwner,OrgCompany,OrgContact,OrgOpp,ContactInfo,CompanyInfo,OppInfo,OppDate,$state,ngDialog,$cookieStore){
    $rootScope.organizationid=$cookieStore.get("_currentOrganizationId");
    //数据结构定义
    $scope.orgContactStruct = [
        {
            name: "基本信息",
            type: "normal",
            blockName: "orgContactBaseInfo",
            level: "top",
            struct: [
                {
                    name: "姓名",
                    key: "name",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                    required: true,
                    rowItemNum: 2,
                    class: "margin-right-p1"
                },
                {
                    name: "别名",
                    key: "alias",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                    rowItemNum: 2,
                    class: "margin-left-p1"
                },
                {
                    name: "英文名",
                    key: "englishName",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                    rowItemNum: 2,
                    class: " margin-right-p1"
                },
                {
                    name: "民族",
                    key: "ethnic",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                    rowItemNum: 2,
                    class: "margin-left-p1"
                },
                {
                    name: "政党",
                    key: "party",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                    rowItemNum: 2,
                    class: "margin-right-p1"
                },
                {
                    name: "血型",
                    key: "blood",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                    rowItemNum: 2,
                    class: "margin-left-p1"
                },
                {
                    name: "Z号",
                    key: "timing",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                    rowItemNum: 2,
                    class: "margin-right-p1"
                },
                {
                    name: "QQ",
                    key: "qq",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                    rowItemNum: 2,
                    class: "margin-left-p1"
                },
                {
                    name: "微信",
                    key: "wechat",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                    rowItemNum: 2,
                    class: "margin-right-p1"
                },
                {
                    name: "宗教",
                    key: "religion",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                    rowItemNum: 2,
                    class: "margin-left-p1"
                },
                {
                    name: "身高",
                    key: "height",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                    rowItemNum: 2,
                    class: "margin-right-p1"
                },
                {
                    name: "体重",
                    key: "weight",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                    rowItemNum: 2,
                    class: "margin-left-p1"
                },
                {
                    name: "基本联系",
                    key: "baseContact",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                },
                {
                    name: "生日",
                    key: "birthDate",
                    option: 2,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                },
                {
                    name: "性别",
                    key: "gender",
                    option: 3,
                    select: [
                        {
                            name: "男",
                            value: 1
                        },
                        {
                            name: "女",
                            value: 0
                        }
                    ],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                    rowItemNum: 2,
                    class: "margin-right-p1"
                },
                {
                    name: "毗邻",
                    key: "isNeighbor",
                    option: 3,
                    select: [{
                        name: "是",
                        value: true
                    }, {
                        name: "否",
                        value: false
                    }],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                    rowItemNum: 2,
                    class: "margin-left-p1"
                },
            ]
        },
        {
            name: "地址列表",
            type: "repeat",
            blockName: "orgContactBaseInfoAddresss",
            preName: "地址",
            struct: [
                {
                    name: "三级地址菜单",
                    key: ["province", "city", "district"],
                    option: 4,
                    select: [],
                    show: [],
                    preview: 5,
                    preBefore: "",
                    preAfter: "",
                },
                {
                    name: "地区",
                    key: "area",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 2,
                    preBefore: "",
                    preAfter: "",
                },
                {
                    name: "街道",
                    key: "street",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 2,
                    preBefore: "",
                    preAfter: "",
                },
                {
                    name: "门牌号",
                    key: "house",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 3,
                    preBefore: "",
                    preAfter: "号",
                },
                {
                    name: "详细地址",
                    key: "detailAddress",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 1,
                    preBefore: ":",
                    preAfter: "",
                },
                {
                    name: "邮编",
                    key: "postcode",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 4,
                    preBefore: "(",
                    preAfter: ")"
                }
            ]
        },
        {
            name: "电话列表",
            type: "repeat",
            blockName: "orgContactBaseInfoMobileNumberList",
            preName: "电话",
            struct: [
                {
                    name: "电话类型",
                    key: "name",
                    option: 3,
                    select: [
                        {
                            name: "默认",
                            value: "默认"
                        },
                        {
                            name: "开放",
                            value: "开放"
                        },
                        {
                            name: "专用",
                            value: "专用"
                        },
                        {
                            name: "家庭",
                            value: "家庭"
                        },
                        {
                            name: "办公",
                            value: "办公"
                        },
                        {
                            name: "私人",
                            value: "私人"
                        },
                    ],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: ":",
                    noneName: true,
                    rowItemNum: 5,
                },
                {
                    name: "号码",
                    key: "value",
                    option: 1,
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                    noneName: true,
                    rowItemNum: 5,
                    itemsPerRow: "four",
                    class: "margin-left-p3"
                }
            ]
        },
        {
            name: "邮件列表",
            type: "repeat",
            blockName: "orgContactBaseInfoEmailList",
            preName: "邮箱",
            struct: [
                {
                    name: "邮箱类型",
                    key: "name",
                    option: 3,
                    select: [
                        {
                            name: "默认",
                            value: "默认"
                        },
                        {
                            name: "开放",
                            value: "开放"
                        },
                        {
                            name: "专用",
                            value: "专用"
                        },
                        {
                            name: "家庭",
                            value: "家庭"
                        },
                        {
                            name: "办公",
                            value: "办公"
                        },
                        {
                            name: "私人",
                            value: "私人"
                        },
                    ],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: ":",
                    noneName: true,
                    rowItemNum: 5,
                },
                {
                    name: "邮箱",
                    key: "value",
                    option: 1,
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                    noneName: true,
                    rowItemNum: 5,
                    itemsPerRow: "four",
                    class: "margin-left-p3"
                }
            ]
        },
        {
            name: "固话/传真列表",
            type: "repeat",
            blockName: "orgContactBaseInfoFixedNumberList",
            preName: "固话",
            struct: [
                {
                    name: "固话类型",
                    key: "name",
                    option: 3,
                    select: [
                        {
                            name: "默认",
                            value: "默认"
                        },
                        {
                            name: "开放",
                            value: "开放"
                        },
                        {
                            name: "专用",
                            value: "专用"
                        },
                        {
                            name: "家庭",
                            value: "家庭"
                        },
                        {
                            name: "办公",
                            value: "办公"
                        },
                        {
                            name: "私人",
                            value: "私人"
                        },
                    ],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: ":",
                    noneName: true,
                    rowItemNum: 5,
                },
                {
                    name: "号码",
                    key: "value",
                    option: 1,
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                    noneName: true,
                    rowItemNum: 5,
                    itemsPerRow: "four",
                    class: "margin-left-p3"
                }
            ]
        },
        {
            name: "商业信息",
            level: "top",
            type: "normal",
            blockName: "orgContactBusinessInfo",
            struct: [
                {
                    name: "业务关系",
                    key: "businessConnection",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                },
                {
                    name: "业务关系类型",
                    key: "businessConnectionKind",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                },
                {
                    name: "友好程度",
                    key: "friendshipToOrg",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                },
                {
                    name: "地位",
                    key: "positionInfo",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                },
                {
                    name: "重要程度",
                    key: "significanceToOrg",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                },
            ]
        },
        {
            name: "雇佣信息",
            type: "normal",
            level: "top",
            blockName: "orgContactEmployeeInfo",
            struct: [
                {
                    name: "部门",
                    key: "department",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                },
                {
                    name: "上级名称",
                    key: "leaderId",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                },
                {
                    name: "等级",
                    key: "level",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                },
                {
                    name: "地位",
                    key: "position",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                },
                {
                    name: "专业类型",
                    key: "professionsType",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                },
                {
                    name: "职责",
                    key: "responsibility",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                },

            ]
        },
        {
            name: "教育经历",
            type: "repeat",
            level: "top",
            blockName: "orgContactEducationHistorys",
            preName: "经历",
            struct: [
                {
                    name: "文凭",
                    key: "diploma",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 3,
                    preBefore: "",
                    preAfter: ":",
                },
                {
                    name: "学校",
                    key: "school",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 2,
                    preBefore: "",
                    preAfter: ""
                },
                {
                    name: "入学日期",
                    key: "entranceDate",
                    option: 2,
                    select: [],
                    show: [],
                    preview: 1,
                    preBefore: "(",
                    preAfter: ""
                },
                {
                    name: "离校日期",
                    key: "graduateDate",
                    option: 2,
                    select: [],
                    show: [],
                    preview: 4,
                    preBefore: "-",
                    preAfter: ")"
                },
                {
                    name: "学院",
                    key: "institute",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: ""
                },
                {
                    name: "系",
                    key: "faculty",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: ""
                },
                {
                    name: "主修",
                    key: "major",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: ""
                },
                {
                    name: "班级",
                    key: "schoolClass",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: ""
                },
            ]
        },
        {
            name: "工作经历",
            type: "repeat",
            level: "top",
            blockName: "orgContactWorkHistorys",
            preName: "经历",
            struct: [
                {
                    name: "地位",
                    key: "position",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 3,
                    preBefore: "",
                    preAfter: ":",
                },
                {
                    name: "公司名称",
                    key: "company",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 2,
                    preBefore: "",
                    preAfter: ""
                },
                {
                    name: "入职日期",
                    key: "beginDate",
                    option: 2,
                    select: [],
                    show: [],
                    preview: 1,
                    preBefore: "(",
                    preAfter: ""
                },
                {
                    name: "离职日期",
                    key: "endDate",
                    option: 2,
                    select: [],
                    show: [],
                    preview: 4,
                    preBefore: "-",
                    preAfter: ")"
                },
                {
                    name: "部门",
                    key: "department",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: ""
                },
                {
                    name: "专长",
                    key: "professionsType",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: ""
                },
                {
                    name: "上司",
                    key: "reportPerson",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: ""
                },
                {
                    name: "职责",
                    key: "responsibility",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: ""
                },
                {
                    name: "等级",
                    key: "level",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: ""
                },
            ]
        },
    ]
    $scope.orgCompanyStruct=[
        {
            name:"基本信息",
            type:"normal",
            blockName:"companyBaseInfo",
            struct:[
                {
                    name:"公司名称",
                    key: "name",
                    option:1,
                    select:[],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:"",
                },
                {
                    name: "公司别名",
                    key: "alias",
                    option:1,
                    select:[],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:"",
                },
                {
                    name:"英文名称",
                    key: "englishName",
                    option:1,
                    select:[],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:"",
                },
                {
                    name:"公司概况",
                    key: "overview",
                    option:1,
                    select:[],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:"",
                },
                {
                    name:"业务关系",
                    key: "businessConnection",
                    option:1,
                    select:[],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:"",
                },
                {
                    name:"友好程度",
                    key: "friendship",
                    option:1,
                    select:[],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:"",
                },
                {
                    name:"重要程度",
                    key: "importance",
                    option:1,
                    select:[],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:"",
                },
                {
                    name:"成立时间",
                    key: "foundDate",
                    option:2,
                    select:[],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:"",
                },
                {
                    name:"注册资本",
                    key: "registerCapital",
                    option:1,
                    select:[],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:"",
                },
                {
                    name:"公司类型",
                    key: "type",
                    option:1,
                    select:[],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:""
                },
                {
                    name:"货币单位",
                    key: "unit",
                    option:1,
                    select:[],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:""
                },
                {
                    name:"主页",
                    key: "webSite",
                    option:1,
                    select:[],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:""
                }
            ]
        },
        {
            name:"地址列表",
            type:"repeat",
            blockName:"companyAddressList",
            preName:"地址",
            struct:[
                {
                    name:"三级地址菜单",
                    key: ["province","city","district"],
                    option:4,
                    select:[],
                    show:[],
                    preview:5,
                    preBefore:"",
                    preAfter:"",
                },
                {
                    name:"地区",
                    key: "area",
                    option:1,
                    select:[],
                    show:[],
                    preview:2,
                    preBefore:"",
                    preAfter:"",
                },
                {
                    name:"街道",
                    key: "street",
                    option:1,
                    select:[],
                    show:[],
                    preview:2,
                    preBefore:"",
                    preAfter:"",
                },
                {
                    name:"门牌号",
                    key: "house",
                    option:1,
                    select:[],
                    show:[],
                    preview:3,
                    preBefore:"",
                    preAfter:"号",
                },
                {
                    name:"详细地址",
                    key: "detailAddress",
                    option:1,
                    select:[],
                    show:[],
                    preview:1,
                    preBefore:":",
                    preAfter:"",
                },
                {
                    name:"邮编",
                    key: "postcode",
                    option:1,
                    select:[],
                    show:[],
                    preview:4,
                    preBefore:"(",
                    preAfter:")"
                }
            ]
        },
        {
            name:"电话列表",
            type:"repeat",
            blockName:"companyPhoneList",
            preName:"电话",
            struct:[
                {
                    name:"热线类型",
                    key:"lineName",
                    option:3,
                    select:[
                        {
                            name:"默认",
                            value:"默认"
                        },
                        {
                            name:"专用",
                            value:"专用"
                        }
                    ],
                    show:[],
                    preview:3,
                    preBefore:"",
                    preAfter:":"
                },
                {
                    name:"热线号码",
                    key:"lineNumber",
                    option:1,
                    show:[],
                    preview:2,
                    preBefore:"",
                    preAfter:""
                }
            ]
        },
        {
            name:"部门列表",
            type:"select",
            blockName:"companyDepartmentList",
        },
    ]
    $scope.orgOppStruct=[
        {
            name:"基本信息",
            type:"normal",
            blockName:"oppBaseInfo",
            struct:[
                {
                    name:"项目名称",
                    key: "oppName",
                    option:1,
                    select:[],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:"",
                    required:true
                },
                {
                    name: "项目代码",
                    key: "oppCode",
                    option:1,
                    select:[],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:"",
                    required:true
                },
                {
                    name:"项目简介",
                    key: "overview",
                    option:1,
                    select:[],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:"",
                },
                {
                    name:"预算",
                    key: "budget",
                    option:1,
                    select:[],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:"",
                },
                {
                    name:"期望收入",
                    key: "expectRevenue",
                    option:1,
                    select:[],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:"",
                },
                {
                    name:"操控类型",
                    key: "controllerType",
                    option:3,
                    select:[
                        {
                            name:"我自己",
                            value:"我自己"
                        },
                        {
                            name:"公司",
                            value:"公司"
                        }],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:"",
                },
                {
                    name:"竞争强度",
                    key: "competition",
                    option:1,
                    select:[
                        {
                            name:"平和",
                            value:"平和"
                        },
                        {
                            name:"激烈",
                            value:"激烈"
                        },
                        {
                            name:"很激烈",
                            value:"很激烈"
                        }
                    ],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:"",
                },
                {
                    name:"现状",
                    key: "reality",
                    option:3,
                    select:[
                        {
                            name:"OPEN",
                            value:"OPEN"
                        },
                        {
                            name:"WIN",
                            value:"WIN"
                        },
                        {
                            name:"LOST",
                            value:"LOST"
                        }
                    ],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:"",
                },
                {
                    name:"属性",
                    key: "attribute",
                    option:3,
                    select:[
                        {
                            name:"LEAD",
                            value:"LEAD"
                        },
                        {
                            name:"OPP",
                            value:"OPP"
                        }
                    ],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:"",
                },
                {
                    name:"胜算",
                    key: "odds",
                    option:1,
                    select:[],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:""
                },
                {
                    name:"需求",
                    key: "requirement",
                    option:1,
                    select:[],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:""
                },
                {
                    name:"货币单位",
                    key: "unit",
                    option:1,
                    select:[],
                    show:[],
                    preview:0,
                    preBefore:"",
                    preAfter:""
                },
                {
                    name:"复杂程度",
                    key: "type",
                    option:1,
                    select:"",
                    show:[
                        {
                            name:"简单",
                            value:"简单"
                        },
                        {
                            name:"一般",
                            value:"一般"
                        },
                        {
                            name:"复杂",
                            value:"复杂"
                        },
                        {
                            name:"很复杂",
                            value:"很复杂"
                        }
                    ],
                    preview:0,
                    preBefore:"",
                    preAfter:""
                }
            ]
        },
        {
            name:"日期信息",
            type:"repeat",
            blockName:"oppDateList",
            preName:"日期",
            struct:[
                {
                    name:"日期类型",
                    key:"name",
                    option:3,
                    select:[
                        {
                            name:"卖标书的日期",
                            value:"卖标书的日期"
                        },
                        {
                            name:"开标日期",
                            value:"开标日期"
                        },
                        {
                            name:"宣布中标结果的日期",
                            value:"宣布中标结果的日期"
                        },
                        {
                            name:"Early Close",
                            value:"Early Close"
                        },
                        {
                            name:"Final Close",
                            value:"Final Close"
                        },
                        {
                            name:"用户发票最晚日期",
                            value:"用户发票最晚日期"
                        },
                        {
                            name:"用户结帐最晚日期",
                            value:"用户结帐最晚日期"
                        }
                    ],
                    show:[],
                    preview:3,
                    preBefore:"",
                    preAfter:":"
                },
                {
                    name:"日期",
                    key:"date",
                    option:2,
                    select:"",
                    show:[],
                    preview:2,
                    preBefore:"",
                    preAfter:""
                }
            ]
        },
        {
            name: "相关联系人",
            type: "select",
            blockName: "oppRelatedContact",
        }
        /*{
         name:"交易",
         type:"repeat",
         blockName:"oppTradesList",
         preName:"交易",
         struct:[
         {
         name:"产品编号",
         key:"productId",
         option:1,
         select:"",
         show:[],
         preview:0,
         preBefore:"",
         preAfter:":"
         },
         {
         name:"折扣",
         key:"discount",
         option:1,
         show:[],
         preview:4,
         preBefore:"(",
         preAfter:"折)"
         },
         {
         name:"count",
         key:"",
         option:1,
         select:"",
         show:[],
         preview:0,
         preBefore:"",
         preAfter:":"
         },
         {
         name:"成交价格",
         key:"finalPrice",
         option:1,
         select:"",
         show:[],
         preview:0,
         preBefore:"",
         preAfter:":"
         }
         ]
         },*/
    ]
    $scope.presentOrgContact;
    $scope.presentOrgCompany;
    $scope.presentOrgOpp;
    //初始化页码
    $scope.numbersPerPage=10;
    $scope.numbersPerPageEnable=[5,8,10,15,20,50];
    $scope.presentPage=1;
    //分页显示控制
    $scope.getPages=function(){
        var list=$rootScope.toState.name=="businessContactManage"?$scope.orgContactList:$rootScope.toState.name=="businessCompanyManage"?$scope.orgCompanyList:$scope.orgOppList
        $scope.number=list==undefined?0:list.length
        $scope.totalPages=Math.ceil($scope.number/$scope.numbersPerPage);
        $scope.pages=[];
        if ($scope.presentPage==$scope.totalPages&&$scope.presentPage>4) $scope.pages.push($scope.presentPage-4);
        if (($scope.presentPage==$scope.totalPages||$scope.presentPage==$scope.totalPages-1)&&$scope.presentPage>3) $scope.pages.push($scope.presentPage-3);
        if ($scope.presentPage>2) $scope.pages.push($scope.presentPage-2);
        if ($scope.presentPage>1) $scope.pages.push($scope.presentPage-1);
        $scope.pages.push($scope.presentPage);
        if ($scope.presentPage<$scope.totalPages) $scope.pages.push($scope.presentPage+1);
        if ($scope.presentPage<$scope.totalPages-1) $scope.pages.push($scope.presentPage+2);
        if (($scope.presentPage==1||$scope.presentPage==2)&&$scope.presentPage<$scope.totalPages-2) $scope.pages.push($scope.presentPage+3);
        if ($scope.presentPage==1&&$scope.presentPage<$scope.totalPages-3) $scope.pages.push($scope.presentPage+4);
    }
    $scope.showRow=function(index){
        $scope.rowStart=($scope.presentPage-1)*$scope.numbersPerPage;
        $scope.rowEnd=$scope.rowStart+$scope.numbersPerPage-1;
        $scope.rowEnd=$scope.rowEnd<$scope.number?$scope.rowEnd:$scope.number-1
        if (index>=$scope.rowStart&&index<=$scope.rowEnd) return true;
        return false;
    }
    $scope.changePage=function(page){
        if (page<1||page>$scope.totalPages) return;
        $scope.presentPage=page;
        $scope.getPages();
    }
    $scope.$watch('numbersPerPage',function(newValue,oldValue, scope){
        $scope.presentPage=1;
        $scope.getPages();
    });
    //切换视图
    $scope.changeTo=function(state){
        if ($rootScope.toState.name==state) return;
        $state.go(state);
    }
    //初始化
    $scope.getAllMembership=function(item){
        var str=[];
        str=!item.innerMembership?str:item.innerMembership.indexOf("*")>=0?str.concat(item.innerMembership.slice(1).split(",")):str.concat(item.innerMembership.split(","))
        str=!item.relativeMembership?str:item.relativeMembership.indexOf("*")>=0?str.concat(item.relativeMembership.slice(1).split(",")):str.concat(item.relativeMembership.split(","))
        str=!item.talentPoolMembership?str:item.talentPoolMembership.indexOf("*")>=0?str.concat(item.talentPoolMembership.slice(1).split(",")):str.concat(item.talentPoolMembership.split(","))
        str=!item.semiMembership?str:item.semiMembership.indexOf("*")>=0?str.concat(item.semiMembership.slice(1).split(",")):str.concat(item.semiMembership.split(","))
        str=!item.businessContactMembership?str:item.businessContactMembership.indexOf("*")>=0?str.concat(item.businessContactMembership.slice(1).split(",")):str.concat(item.businessContactMembership.split(","))
        return str;
    }
    $scope.loadOrgContact=function() {
        OrgContact.query({page: 0, size: 5000, sort: ['id' + ',' + 'asc', 'id']}, function (result) {
            $scope.orgContactAllList=result;
            $scope.orgContactList=_.filter(result,function(item){
                return item.businessContactMembership&&item.businessContactMembership.indexOf("*")==0;
            });
            $scope.orgContactKeyList=_.keyBy(result,"id");
            _.forEach($scope.orgContactKeyList,function(item){
                item.$$memberships = $scope.getAllMembership(item)
            })
            if($rootScope.toState.name=="businessContactManage") $scope.getPages();
        },function(){
            $scope.orgContactList=[];
        })
    }
    $scope.loadOrgCompany=function(){
        OrgCompany.queryByOrgnizationPaging({infoid:$rootScope.organizationid,page:0, size: 5000, sort: ['id' + ',' + 'asc', 'id']},function(result){
            $scope.orgCompanyList=result;
            _.forEach($scope.orgCompanyList,function(item){
                item.$$memberships=item.membership?item.membership.split(","):[]
            })
            if($rootScope.toState.name=="businessCompanyManage") $scope.getPages();
        },function(){
            $scope.orgCompanyList=[];
        })
    }
    $scope.getAllSubOpp=function(array){
        $scope.orgOppList=[];
        $rootScope.organizationId=$cookieStore.get("_currentOrganizationId");
        Member.query({orgid:$rootScope.organizationId,page: 0, size: 5000, sort: ['id' + ',' + 'asc', 'id']},function(result){
            $scope.memberList=result;
            $scope.memberKeyList=_.keyBy(result,"orgContactId");
            $scope.login=$cookieStore.get("user").data.login;
            _.forEach($scope.memberList,function(item){
                if (item.user&&item.user.login==$scope.login){
                    $scope.orgContactId=item.orgContactId;
                }
            })
            _.forEach($scope.orgContactAllList,function(item){
                var df=$q.defer();
                var total=0;
                var resolve=0;
                if (item.employeeInfo&&item.employeeInfo.leaderId==$scope.orgContactId){
                    total=total+1;
                    if ($scope.memberKeyList[item.id]) {
                        OrgOpp.entityOwner({infoid: $scope.memberKeyList[item.id].id}, function (result) {
                            var eos = _.keyBy(result, function (item) {
                                return item.managedEntity.id
                            })
                            _.forEach($scope.oppAllList, function (item) {
                                if (eos[item.businessEntity.id]) {
                                    item.$$owner = eos[item.businessEntity.id].member.name;
                                    $scope.orgOppList.push(item)
                                }
                            })
                            resolve = resolve + 1;
                            if (resolve == total) df.resolve();
                        }, function () {
                            resolve = resolve + 1;
                            if (resolve == total) df.resolve();
                        })
                    }
                    else resolve=resolve+1;
                }
                df.promise.then(function(){
                    $scope.addTotalForecast();
                    if($rootScope.toState.name=="businessOpportunityManage") $scope.getPages();
                })
            })
        })
    }
    $scope.loadOrgOpportunity=function(){
        OrgOpp.queryByOrgnization({infoid:$rootScope.organizationid,page:0, size: 5000, sort: ['id' + ',' + 'asc', 'id']},function(result){
            $scope.oppAllList=result;
            $scope.getAllSubOpp(result);
            //if($rootScope.toState.name=="businessOpportunityManage") $scope.getPages();
        },function(){
            $scope.orgOppList=[];
        })
    }
    $scope.loadUserContact=function(){
        ContactInfo.query({page: 0, size: 5000, sort: ['id' + ',' + 'asc', 'id']}, function (result) {
            $scope.userContactList = [];
            _.forEach(result,function(item){
                ContactInfo.queryAll({infoid:item.id},function(r){
                    $scope.userContactList.push(r);
                })
            })
        })
    }
    $scope.loadUserCompany=function() {
        CompanyInfo.query({infoid:'all',page: 0, size: 5000, sort: ['id' + ',' + 'asc', 'id']}, function (result) {
            $scope.userCompanyList = result;
        })
    }
    $scope.loadUserOpportunity=function(){
        OppInfo.query({infoid:'all',page:0, size: 5000, sort: ['id' + ',' + 'asc', 'id']},function(result) {
            $scope.userOppList = result;
        })
    }
    $scope.loadAll=function(){
        $scope.loadOrgContact();
        $scope.loadOrgCompany();
        $scope.loadOrgOpportunity();
        $scope.loadUserContact();
        $scope.loadUserCompany();
        $scope.loadUserOpportunity();
    }
    $scope.loadAll();
    Membership.queryAll({page: 0, size: 5000, sort: ['id' + ',' + 'asc', 'id']},function(result){
        $scope.allMembership=result;
        $scope.allMembershipIdList=_.keyBy(result,"id")
    })
    //LinkUp and Linkdown
    $scope.linkDown=function(){
        var df=$q.defer();
        var resolve=0;
        ContactInfo.linkDown(function(){
            resolve=resolve+1;
            if (resolve==3) df.resolve();
        });
        CompanyInfo.linkDown(function(){
            resolve=resolve+1;
            if (resolve==3) df.resolve();
        });
        OppInfo.linkDown(function(){
            resolve=resolve+1;
            if (resolve==3) df.resolve();
        });
        df.promise.then(function(){
            alert("同步成功")
            $scope.loadAll();
        })
    }
    $scope.linkupContact=function(item,yes){
        console.log(item.linkedOrgContactId)
        if (!item.linkedOrgContactId) ContactInfo.linkup({infoid:item.id},function(){
            alert("链接成功")
            $scope.loadUserContact();
        })
        else{
            ContactInfo.linkupConfirm({infoid:item.id,orgContactId:item.linkedOrgContactId,perferOrg:yes},{},function(){
                alert("操作成功")
                $scope.loadUserContact();
            })
        }
    }
    $scope.breakLinkContact=function(item){
        if (!confirm("确定要解除与公司的链接么")) return;
        ContactInfo.breakLink({infoid:item.id},function(){
            alert("解除成功")
            $scope.loadUserContact();
        })
    }
    $scope.linkupCompany=function(item,yes){
        CompanyInfo.linkup({infoid:item.id,organizationId:$rootScope.organizationId,perferOrg:yes},function(){
            alert("操作成功")
            $scope.loadUserCompany();
        })
    }
    $scope.breakLinkCompany=function(item){
        if (!confirm("确定要解除与公司的链接么")) return;
        CompanyInfo.breakLink({infoid:item.id},function(){
            alert("解除成功")
            $scope.loadUserCompany();
        })
    }
    $scope.linkupOpp=function(item,yes){
        OppInfo.linkup({infoid:item.id,organizationId:$rootScope.organizationId,perferOrg:yes},function(){
            alert("操作成功")
            $scope.loadUserOpportunity();
        })
    }
    $scope.breakLinkOpp=function(item){
        if (!confirm("确定要解除与公司的链接么")) return;
        OppInfo.breakLink({infoid:item.id},function(){
            alert("解除成功")
            $scope.loadUserOpportunity();
        })
    }
    //排序
    $scope.reverse=false;
    $scope.orderByKey=function(key,eve){
        if (eve.toElement.childElementCount==0) return;
        $scope.reverse = ($scope.orderKey === key) ? !$scope.reverse : false;
        $scope.orderKey = key;
    }
    $scope.orderList=function(item){
        return item.baseInfo?pinyin.getFullChars(item.baseInfo.name):
            item.companyBaseInfo?pinyin.getFullChars(item.companyBaseInfo.name):
                item.opportunity?pinyin.getFullChars(item.opportunity.oppName):""
    }
    //筛选
    $scope.filterInfos={
        name:"",
        membership:"客户"
    };
    $scope.infosFilter=function(item){
        if (item.isMe) return false;
        if ($scope.filterInfos.name!="") {
            if (item.baseInfo) {
                var result=false;
                if (item.baseInfo.name.indexOf($scope.filterInfos.name) >= 0) result=true;
                if (item.baseInfo.mobileNumberList != undefined && item.baseInfo.mobileNumberList.length > 0 && item.baseInfo.mobileNumberList[0].value.indexOf($scope.filterInfos.name) >= 0) result=true;
                if (item.baseInfo.emailList != undefined && item.baseInfo.emailList.length > 0 && item.baseInfo.emailList[0].value.indexOf($scope.filterInfos.name) >= 0) result=true;
                return result;
            }
            else if (item.companyBaseInfo){
                if (item.companyBaseInfo.name.indexOf($scope.filterInfos.name) < 0) return false
            }
            else if (item.opportunity){
                if (item.opportunity.oppName.indexOf($scope.filterInfos.name) < 0) return false
            }
            else return false
        }
        return true;
    }
    //计算总预算
    $scope.addOneForecast=function(item){
        $scope.orgOppList.$$totalForecast=!item.opportunity.budget?$scope.orgOppList.$$totalForecast:$scope.orgOppList.$$totalForecast+item.opportunity.budget
    }
    $scope.addTotalForecast=function(){
        if($scope.orgOppList) {
            $scope.orgOppList.$$totalForecast = 0;
            _.forEach(_.filter($scope.orgOppList, $scope.infosFilter), function (item) {
                $scope.addOneForecast(item)
            })
        }
    }
    $scope.$watch(function(){
        return $scope.filterInfos.name;
    },function(){
        $scope.addTotalForecast();
    })
})